<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>草稿箱</title>

    <!-- Bootstrap -->
    <link href="/resource/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/resource/css/dropdowns-enhancement.css"/>
    <link rel="stylesheet" href="/resource/css/bootstrap-table.min.css"/>
    <link rel="stylesheet" href="/resource/css/toastr.min.css"/>
    <link rel="stylesheet" href="/resource/css/admin.css"/>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<% include ../Admin/include/nav.html %> <!--顶部导航-->
<div class="container">
    <div class="row">
        <div class="col-sm-3 col-md-2">
            <% include ../Admin/include/sidebar.html %> <!--左侧菜单-->

        </div>
        <div class="col-sm-9 col-md-10 main">
            <h4 class="page-header"><span aria-hidden="true" class="glyphicon glyphicon-inbox"></span>  草稿箱</h4>
            <div class="btn-group ">
                <button data-label-placement="" data-placeholder="Please select"
                        class="btn btn-default">全部分类
                </button>
                <button class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span
                        class="caret"></span></button>
                <ul class="dropdown-menu" id="load-data">
                    <li>
                        <input type="radio" checked="" value="" name="cateid" id="cateid2_2_1">
                        <label for="cateid2_2_1"><i class="icon-edit"></i> 全部分类</label>
                    </li>
                    <li>
                        <input type="radio" value="0" name="cateid" id="cateid2_2_1a">
                        <label for="cateid2_2_1a"><i class="icon-edit"></i> 未分类</label>
                    </li>
                    <li role="presentation" class="divider"></li>
                    <% category.forEach(function(item){ %>
                    <li>
                        <input type="radio" value="<%=item.cid%>" name="cateid" id="cid<%=item.cid%>_<%=item.pid%>_<%=item.deep%>">
                        <label for="cid<%=item.cid%>_<%=item.pid%>_<%=item.deep%>"><i class="icon-remove"></i>|-<%=new Array(item.deep*2).join("-")%> <%=item.catename%></label>
                    </li>
                    <% }) %>

                </ul>
            </div>
            <div class="btn-group">
                <button class="btn btn-default" id="check-all" data-method="checkAll">全选</button>
                <button class="btn btn-default" id="uncheck-all" data-method="uncheckAll">取消</button>
            </div>
            <button class="btn btn-default" id="delete">删除</button>
            <button class="btn btn-default" id="draft">发布文章</button>
            <div style="margin-top: -45px">
            <table id="table"
                       data-toggle="table"
                       data-url="/admin/article/data/hide/on"
                       data-show-columns="true"
                       data-search="true"
                       data-show-refresh="true"
                       data-show-toggle="true"
                       data-pagination="true"
                       data-side-pagination="server"
                       data-page-list="[5, 10, 20, 50, 100, 200]"
                       data-page-size="20"
                       >
                    <thead>
                    <tr>
                        <th data-field="state" data-checkbox="true"></th>
                        <th data-field="gid" data-formatter="idFormatter">ID</th>
                        <th data-field="title">标题</th>
                        <th data-field="username">作者</th>
                        <th data-field="type">分类</th>
                        <th data-field="date">时间</th>
                        <th data-field="operate" data-formatter="operateFormatter" data-events="operateEvents">编辑</th>
                    </tr>
                    </thead>
                </table>
            </div>


        </div>
    </div>
</div>
<% include ../Admin/include/footer.html %>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="/resource/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/resource/js/bootstrap.min.js"></script>
<script src="/resource/js/dropdowns-enhancement.min.js"></script>
<script src="/resource/js/bootstrap-table.js"></script>
<script src="/resource/js/bootstrap-table-zh-CN.js"></script>
<script src="/resource/js/bootbox.min.js"></script>
<script src="/resource/js/toastr.min.js"></script>
<script>
    $(function(){
        toastr.options = {
            "timeOut": "1500",
            "closeButton": true,
            "positionClass": "toast-bottom-right"
        };
        if(<%=success%>==1)
        {
            toastr.success('保存成功！');
        }
        $('#load-data>li').click(function () {
            var cid=$(this).find("input").val();
            $("#table").bootstrapTable('refresh', {
                url: '/admin/article/data/hide/on?cid='+cid
            });
        });
        $('#check-all').click(function () {
            $("#table").bootstrapTable("checkAll");
        });
        $('#uncheck-all').click(function () {
            $("#table").bootstrapTable("uncheckAll");
        });
        $('#delete').click(function(){
            var sele=$("#table").bootstrapTable('getSelections');
            if(sele.length==0){
                bootbox.alert({
                    size:'small',
                    message:"您还没有选择文章！"
                })
            }else{
                var gid=[];
                sele.forEach(function(item){
                    gid.push(item.gid);
                })
                gid=gid.join(",");
                bootbox.confirm({
                    size: 'small',
                    message: "你确定要删除所选文章吗？",
                    callback: function (result) {
                        if (result) {
                            $.ajax({
                                url: '/admin/article/dele',
                                data: {gid: gid},
                                type: "GET",
                                dataType: "json",
                                success: function (data) {
                                    $("#table").bootstrapTable('refresh');
                                    toastr.success('成功删除' + data + '篇文章！');
                                    console.log(data);
                                }
                            });

                        }

                    }
                });
            }
        });
        $('#draft').click(function(){
            var sele=$("#table").bootstrapTable('getSelections');
            if(sele.length==0){
                bootbox.alert({
                    size:'small',   message:"您还没有选择文章！"
                })
            }else{
                var gid=[];
                sele.forEach(function(item){
                    gid.push(item.gid);
                })
                gid=gid.join(",");
                bootbox.confirm({
                    size: 'small',
                    message: "你确定要发布所选文章吗？",
                    callback: function (result) {
                        if (result) {
                            $.ajax({
                                url: '/admin/article/todraft',
                                data: {gid: gid,hide:"off"},
                                type: "GET",
                                dataType: "json",
                                success: function (data) {
                                    $("#table").bootstrapTable('refresh');
                                    toastr.success('成功发布' + data + '篇文章！');
                                    console.log(data);
                                }
                            });

                        }

                    }
                });
            }
        });
    });
    function operateFormatter(value, row, index) {
        return [
            '<a class="like" href="javascript:void(0)" title="发布文章">',
            '<i class="glyphicon glyphicon-list-alt"></i>',
            '</a>',
            '&nbsp;&nbsp;<a href="/admin/article/edit/gid/'+row.gid+'" class="edit ml10" href="javascript:void(0)" title="编辑">',
            '<i class="glyphicon glyphicon-edit"></i>',
            '</a>',
            '&nbsp;&nbsp;<a class="remove ml10" href="javascript:void(0)" title="删除">',
            '<i class="glyphicon glyphicon-remove"></i>',
            '</a>'
        ].join('');
    }

    window.operateEvents = {
        'click .like': function (e, value, row, index) {
            var gid = row.gid;
            bootbox.confirm({
                size: 'small',
                message: "你确定要发布所选文章吗？",
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            url: '/admin/article/todraft',
                            data: {gid: gid, hide: "off"},
                            type: "GET",
                            dataType: "json",
                            success: function (data) {
                                $("#table").bootstrapTable('refresh');
                                toastr.success('成功发布' + data + '篇文章！');
                                console.log(data);
                            }
                        });

                    }

                }
            });
        },
//        'click .edit': function (e, value, row, index) {
//            alert('You click edit icon, row: ' + JSON.stringify(row));
//            console.log(value, row, index);
//        },
        'click .remove': function (e, value, row, index) {
            var gid = row.gid;
            bootbox.confirm({
                size: 'small',
                message: "你确定要删除所选文章吗？",
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            url: '/admin/article/dele',
                            data: {gid: gid},
                            type: "GET",
                            dataType: "json",
                            success: function (data) {
                                $("#table").bootstrapTable('refresh');
                                toastr.success('成功删除' + data + '篇文章！');
                                console.log(data);
                            }
                        });

                    }

                }
            });
        }
    }
</script>
</body>
</html>